<?php
namespace api\modules\v1\controllers;

use api\common\controllers\AController;
use common\records\Record;
use common\records\Toy;
use common\records\ToyMachine;
use yii\data\ActiveDataProvider;

class ToyController extends AController
{
    public $modelClass = 'common\records\Toy';
    /**
     * @api {get} /v1/toys 娃娃列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup toys 
     *
     *
     */
    public function actionIndex()
    {
        $result = array();
        if(array_key_exists("uid", $_GET) && !array_key_exists("toy_id", $_GET))
        {
            $uid = $_GET['uid'];
            //$query = Record::find()->select('toy_id,status')->distinct()->where(['uid' => $uid, 'type' => 2])->asArray()->all();

            $sql = "select  r.toy_id, t.name, t.photo, r.status, r.timestamp from record as r, toy as t where t.id = r.toy_id and r.uid = '".$uid."' ORDER BY r.id DESC";
            $toys = Record::findBySql($sql)->asArray()->all();
            $toy_status = array();
            $toy_time = array();
            foreach($toys AS $item)
            {
                $toy_time[$item['toy_id']] = \common\helpers\Util::tranTime($item['timestamp']);
                if(array_key_exists($item['toy_id'], $toy_status))
                {
                    if($item['status'] == 1)
                    {
                        $toy_status[$item['toy_id']] = $toy_status[$item['toy_id']] + 1;
                    }
                }else{
                    $toy_status[$item['toy_id']] = 0;//$item['status'];
                }
            }
            $toy_list = array();
            $toy_pushed = array();
            foreach($toys AS $item)
            {
                if(!array_key_exists($item['toy_id'], $toy_pushed))
                {
                    if($toy_status[$item['toy_id']] > 0)
                    {
                        $item['status'] = 1;
                        $item['num'] = $toy_status[$item['toy_id']];
                        $item['timestamp'] = $toy_time[$item['toy_id']];
                    }else{
                        $item['status'] = 0;//$toy_status[$item['toy_id']];
                        $item['num'] = 0;
                        $item['timestamp'] = $toy_time[$item['toy_id']];
                    }
                    $toy_list[] = $item;
                    $toy_pushed[$item['toy_id']] = 1;
                }
            }
            $result['items'] = $toy_list;
        } else if (array_key_exists("toy_id", $_GET) && array_key_exists("uid", $_GET))
        {

            $toy_id = $_GET['toy_id'];
            $uid = $_GET['uid'];
            $toy = Toy::find()->where(['id'=>$toy_id])->asArray()->one();
            /*$machines = ToyMachine::find()->where(['toy_id'=>$toy_id])->all();

            $location="";
            $locations = array();
            foreach($machines AS $item)
            {
                if($item['address'] != "")
                {
                    $locations[] = $item['address'];
                }else{
                    $locations[] = $item['description'];
                }
            }
            $location = implode(",", $locations);
*/

            $records = Record::find()->where(['uid'=>$uid,'toy_id'=> $toy_id, 'type'=>2])->all();
            $record_list = array();

            $last_machine = null;
            foreach($records AS $record){
                $item = array();
                $item['id'] = $record->id;
                $item['timestamp'] = $record->timestamp;
                $item['description'] = $record->description;
                if($record->status == 2 || $record->status == 0)
                {
                    $item['status'] = 0;

                }else{
                    $item['status'] = 1;
                }
                $last_machine = $record->machine_id;
                $record_list[] = $item;
            }
            $location = "";
            if($last_machine != null)
            {
                $machine =  ToyMachine::find()->where(['mid'=>$last_machine])->one();

                if($machine != null)
                    $location = $machine->address;
            }
            $result['toy'] = $toy;
            $result['records']= $record_list;
            $result['location'] = $location;

        }
        return $result;
    }
}
